//
//  HomeViewController.swift
//  FridgeSolo
//
//  Created by LiuXing on 2017/6/10.
//  Copyright © 2017年 kumi.com. All rights reserved.
//

import UIKit

class HomeViewController: UIViewController {
    
    let sectionItem: [String] = ["Breakfast", "Lunch", "Dinner"]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupUI();
    }
    
    private func setupUI() {
        view.backgroundColor = UIColor.white
        title = "Fridge Solo"
        
        view.addSubview(topView)
        topView.snp.makeConstraints { (make) in
            make.left.right.top.equalTo(view)
            make.height.equalTo(25)
        }
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(view)
            make.top.equalTo(topView.snp.bottom)
        }
        
        tableView.tableHeaderView = headView
    }
    
    private lazy var topView: HomeTopView = {
        let view = HomeTopView.topView()
        return view
    }()
    
    private lazy var headView: HomeHeadView = {
        let view = HomeHeadView.init(frame: CGRect(x: 0, y: 25, width: ScreenWidth, height: 100))
        return view
    }()
    
    // MARK: - lazy
    lazy var tableView: UITableView = {
        let tableView = UITableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
        tableView.separatorStyle = .none
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "2323")
        tableView.dataSource = self
        tableView.delegate = self
        return tableView
    }()
    
}

extension HomeViewController: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView.init(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 40))
        view.backgroundColor = UIColor.black
        let label = UILabel()
        label.textColor = UIColor.white
        view.addSubview(label)
        label.snp.makeConstraints { (make) in
            make.centerY.equalTo(view.snp.centerY)
            make.left.equalTo(view.snp.left).offset(10)
        }
        //
        label.text = sectionItem[section]
        return view
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = UIView.init(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 65))
        let button = UIButton()
        button.setTitle("  Add Food", for: .normal)
        button.setTitleColor(UIColor.colorWithHexString(hex: "3F70BB"), for: .normal)
        button.setImage(UIImage(named: "add"), for: .normal)
        // action
        button.tag = section
        button.addTarget(self, action: #selector(HomeViewController.addFoodButtonClick), for: .touchUpInside)
        button.sizeToFit()
        view.addSubview(button)
        button.snp.makeConstraints { (make) in
            make.top.equalTo(view.snp.top).offset(5)
            make.left.equalTo(view.snp.left).offset(10)
            make.height.equalTo(40)
        }
        let moreView = UIImageView(image: UIImage(named: "goods_more_image"))
        view.addSubview(moreView)
        moreView.snp.makeConstraints { (make) in
            make.centerY.equalTo(button.snp.centerY)
            make.right.equalTo(view.snp.right).offset(-10)
        }
        
        let grayView = UIView()
        grayView.backgroundColor = UIColor.lightGray
        grayView.alpha = 0.8
        view.addSubview(grayView)
        grayView.snp.makeConstraints { (make) in
            make.bottom.left.right.equalTo(view)
            make.top.equalTo(button.snp.bottom).offset(5)
        }
        
        return view
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 65
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "2323", for: indexPath)
        cell.textLabel?.text = "232323"
        return cell
    }
}

extension HomeViewController {
    func addFoodButtonClick(btn: UIButton) {
        let vc = SearchFoodViewController()
        vc.titleString = sectionItem[btn.tag]
        navigationController?.pushViewController(vc, animated: true)
    }
}
